package com.xj;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.JSON;
import com.xj.entity.EncryptionHolder;
import com.xj.isv.service.OpenApiRequestService;
import com.xj.openapi4j.service.*;
import com.xj.openapi4j.service.token.YsToken;
import com.xj.openapi4j.util.HttpUtil;
import com.xj.server.serviceInterface.TransfersSaleOrderInterface;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.RestTemplate;

import java.util.List;


@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class YonSuiteApi {
     @Autowired
    YsToken ysToken;
    @Autowired
    PriceJustService priceJustService;
    @Autowired
    NoteService noteService;
    @Autowired
    TransfersSaleOrderInterface transfersSaleOrderInterface;
    @Autowired
    StockService stockService;
    @Autowired
    InventoryService inventoryService;
    @Autowired
    UnitService unitService;
    @Autowired
    DispatchService dispatchService;

    @Test
    public void  getUserInfo() throws Exception{
      String str=  noteService.getYhtUsridByname("刘阳");
    }

    @Test
    public void autoDelDispatchlist() throws Exception {
        transfersSaleOrderInterface.transSaleorder("XXD241106293");
    }

    @Test
    public void transstock() throws Exception {
        dispatchService.transStockBySaleout();
    }

    @Test
    public void transInfo() throws Exception {
        transfersSaleOrderInterface.autoTransSaleOrder();
    }

    @Test
    public  void  transUnit() throws Exception{
        unitService.translateUnit2YS("9999","块1");
    }

    @Test
    public  void  transJustprice() throws Exception{
        priceJustService.transPriceJust("000000000002");
    }

    @Test
    public  void  autoDelCancelAuditOrder() throws Exception{
        transfersSaleOrderInterface.autoDelCancelAuditOrder();
    }

    @Test
    public void postRequest() throws Exception {
        //   String url="http://nn2eg3068075.vicp.fun/saleorder";
        String url = "http://192.168.3.10:8082/order";
        RestTemplate restTemplate = new RestTemplate();
        EncryptionHolder requestEntity = new EncryptionHolder();
        requestEntity.setNonce("john");
        // 设置请求头信息
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        // 创建请求实体
        HttpEntity<String> entity = new HttpEntity<>(JSON.toJSONString(requestEntity), headers);
        // 发送POST请求
        ResponseEntity<String> response = restTemplate.postForEntity(url, entity, String.class);
        // 输出响应
        System.out.println(response.getBody());
    }

    //YS库存，一个商品有两条记录，通过接口删除
     @Test
    public  void  dowithErrorStockData() throws  Exception{
        String  str="121272";
        String[] invcodeArr=str.split(",");
         String token=ysToken.getToken();
        for(String  invcode:invcodeArr){
            String requestUrl="https://c3.yonyoucloud.com/iuap-api-gateway/yonbip/scm/stockanalysis/list?access_token="+token;
            JSONObject jsonObject=new JSONObject();
              jsonObject.put("pageSize",500);
              jsonObject.put("pageIndex",1);
              jsonObject.put("product_cCode",invcode.trim());
              String respon= HttpUtil.post(requestUrl,jsonObject.toJSONString());
             JSONObject j1=JSONObject.parseObject(respon);
             JSONArray recordList=j1.getJSONObject("data").getJSONArray("recordList");
             for(int i=0;i<recordList.size();i++){
                 JSONObject data=recordList.getJSONObject(i);
                 Long id=data.getLongValue("id");

                 String url="https://c3.yonyoucloud.com/iuap-api-auth/yonbip/scm/stockanalysis/forcesave?access_token="+token;
                 JSONObject param=new JSONObject();
                 JSONObject body = new JSONObject();
                 param.put("_status", "Insert");
                 param.put("org", "001");
                 param.put("warehouse", "bzp");
                 param.put("product", invcode);
                 param.put("productsku", invcode);
                 param.put("currentqty", 0);
                 param.put("id__pk",id);
                 JSONArray result=new JSONArray();
                 result.add(param);
                 body.put("data",result);
                 body.put("key","id__pk");
                 String str1= HttpUtil.post(url,body.toJSONString());
                 System.out.println(str1);
             }
        }

     }

}
